package com.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dao.ChanpinxinxiDao;
import com.entity.ChanpinxinxiEntity;
import com.entity.GongyingshangEntity;
import com.entity.view.ChanpinxinxiView;
import com.entity.view.GongyingshangView;
import com.entity.vo.ChanpinxinxiVO;
import com.service.ChanpinxinxiService;
import com.service.GongyingshangService;
import com.utils.PageUtils;
import com.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("chanpinxinxiService")
public class ChanpinxinxiServiceImpl extends ServiceImpl<ChanpinxinxiDao, ChanpinxinxiEntity> implements ChanpinxinxiService {

    @Autowired
    private GongyingshangService gongyingshangService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ChanpinxinxiEntity> page = this.selectPage(
                new Query<ChanpinxinxiEntity>(params).getPage(),
                new EntityWrapper<ChanpinxinxiEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Wrapper<ChanpinxinxiEntity> wrapper) {
        Page<ChanpinxinxiView> page = new Query<ChanpinxinxiView>(params).getPage();
        List<ChanpinxinxiView> chanpinxinxiViews = baseMapper.selectListView(page, wrapper);
        chanpinxinxiViews.forEach(item -> {
            if (item.getGongyingshangId() != null) {
                GongyingshangView vo = gongyingshangService.selectView(new EntityWrapper<GongyingshangEntity>().eq("id", item.getGongyingshangId()));

                if (vo != null) {
                    item.setGongyingshangName(vo.getGongyingshangmingcheng());
                }
            }
        });
        for (ChanpinxinxiView chanpinxinxiView : chanpinxinxiViews) {
            String chanpinbianhao = chanpinxinxiView.getChanpinbianhao();
            //chauxn原料
            chanpinxinxiView.setYuanliao(baseMapper.selecttyuanliao(chanpinbianhao));
            chanpinxinxiView.setCangchu(baseMapper.selectcangchu(chanpinbianhao));
            chanpinxinxiView.setSelectshengchansuyuan(baseMapper.selectshengchansuyuan(chanpinbianhao));

        }
        page.setRecords(chanpinxinxiViews);
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }

    @Override
    public List<ChanpinxinxiVO> selectListVO(Wrapper<ChanpinxinxiEntity> wrapper) {
        return baseMapper.selectListVO(wrapper);
    }

    @Override
    public ChanpinxinxiVO selectVO(Wrapper<ChanpinxinxiEntity> wrapper) {
        return baseMapper.selectVO(wrapper);
    }

    @Override
    public List<ChanpinxinxiView> selectListView(Wrapper<ChanpinxinxiEntity> wrapper) {
        return baseMapper.selectListView(wrapper);
    }

    @Override
    public ChanpinxinxiView selectView(Wrapper<ChanpinxinxiEntity> wrapper) {
        return baseMapper.selectView(wrapper);
    }


}
